package com.ge.med.cchelper.decorator;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ILightweightLabelDecorator;

import com.ge.med.cchelper.Constants;
import com.ge.med.cchelper.bean.CSession;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.ReourceHelper;
import com.ge.med.cchelper.util.TextHelper;

public class CCDecorator implements ILightweightLabelDecorator {

	public void addListener(ILabelProviderListener arg0) {
	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public boolean isLabelProperty(Object arg0, String arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public void removeListener(ILabelProviderListener arg0) {
		// TODO Auto-generated method stub

	}

	public void decorate(Object element, IDecoration decoration) {
		if (element instanceof IFile) {
			IFile compUnit = (IFile) element;
			String allPath = compUnit.getFullPath().toString();

			IProject currentProject = compUnit.getProject();
            IPath projectIPath = currentProject.getLocation();
            if(projectIPath!=null){
				String projectPath = projectIPath.toOSString();
				try {
					if (TextHelper.isExists(projectPath + File.separator
							+ TextHelper.CHECKOUT_FILE, allPath)) {
						decoration.addSuffix(Constants.CHECKOUT_FLAG);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
            }
			
		} else if (element instanceof IFolder) {
			ReourceHelper resource = ReourceHelper.getResource();
			IFolder currentFolder = (IFolder) element;
			IProject currentProject = currentFolder.getProject();
			CSession cs = resource.getSession(currentProject.getName());
			if (cs != null) {
				if (cs.getHostSession() != null) {
					if (currentFolder.getName().equalsIgnoreCase(Constants.VOB)) {
						decoration.addSuffix("[" + cs.getViewName() + "]");
						decoration.setForegroundColor(LogUtil.redColor);
						decoration.addOverlay(ImageDescriptor.createFromFile(
								CCDecorator.class, "/icon/vob.gif"),
								IDecoration.TOP_LEFT);
					}
				}
			}
			
			String allPath = currentFolder.getFullPath().toString();
			try {
				if (TextHelper.isExistsLikePath(currentProject.getLocation().toOSString() + File.separator
						+ TextHelper.CHECKOUT_FILE, allPath)) {
					decoration.addSuffix(Constants.CHECKOUT_FLAG);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			

		} else if (element instanceof IProject) {
			ReourceHelper resource = ReourceHelper.getResource();
			IProject currentProject = (IProject) element;
			CSession cs = resource.getSession(currentProject.getName());
			if (cs != null) {
				if (cs.getHostSession() != null) {
					decoration.addSuffix("[connected to "
							+ cs.getHost().getIp() + "]");
				}
			}
		}
	}
}
